package pcb.control.responders {
	import com.intel.core.Application;
	import com.intel.service.ServiceEvent;
	import com.intel.utils.Debugger;
	import flash.display.StageAlign;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import pcb.control.events.LoadDataEvent;
	import pcb.models.LessonStateModel;
	import pcb.models.PlayerSettings;
	import pcb.scorm.SCORMRuntime;

	/**
	 * ...
	 * @author Fintan Boyle
	 */
	public class Re_loadPlayerConfig extends AbstractResponder {
		private var playerType:String;

		public function Re_loadPlayerConfig(playerType:String = "") {
			trace("playerType " + playerType);
			if (playerType != "")
				this.playerType = playerType;
		}

		override public function result(e:ServiceEvent):void {
			Debugger.traceOut("Re_loadLessonConfig result");

			var data:XML = XML(e.data);
			
			if (data.scorm != null) {				
				models.playerSettings.useScorm = (data.scorm == "true") ? true : false;
				if (models.playerSettings.useScorm) {
					
					SCORMRuntime.getInstance().connect();
					
					models.playerSettings.scormLessonId = data.scorm_lesson_id;
					
				}
				
			}

			models.playerSettings.playerType = this.playerType;

			models.playerSettings.autoStartLesson = (data.playback.autoPlay == "true") ? true : false;
			models.playerSettings.audioQuality = data.playback.audio;
			models.playerSettings.course = data.course;

			var delay:Number = data.playback.slidetransitions.delay as Number;
			models.playerSettings.slideChangeDelay = (isNaN(delay)) ? delay : 1000;

			models.playerSettings.autoSlideTransition = data.playback.slidetransitions.auto == "true" ? true : false;
			
			//daveh - replacing to have multiple audio settings
			//models.playerSettings.playAudio = (data.playback.audio == PlayerSettings.HIGH_QUALITY_SOUND || data.playback.audio == PlayerSettings.LOW_QUALITY_SOUND) ? true : false;
			models.playerSettings.playAudio = data.playback.audio != PlayerSettings.NO_SOUND;

			
			//currently coupling use of menu with any player type that isn't a single lesson
			//models.playerSettings.useMainMenu = data.controls.useMainMenu == "true"?true:false;
			
			models.playerSettings.useMainMenu = (models.playerSettings.playerType != PlayerSettings.SINGLE_LESSON) ? true : false;

			models.playerSettings.culture = data.culture;
			
			trace("$$$$$$$$$$$$$$$$$$data.culture " + data.culture);

			models.playerSettings.deployment = data.player.deploy;
			models.playerSettings.scaleToFit = (data.player.dimensions.scale == "true") ? true : false;
			models.playerSettings.fullscreenMode = (data.player.dimensions.fullscreen == "true") ? true : false;

			if (models.playerSettings.scaleToFit){
				Application.stage.align = StageAlign.TOP;
				Application.stage.scaleMode = StageScaleMode.SHOW_ALL;

			} else {
				Application.stage.align = StageAlign.TOP_LEFT;
				Application.stage.scaleMode = StageScaleMode.NO_SCALE;
			}

			if (models.playerSettings.fullscreenMode && models.playerSettings.deployment != "web"){

				if (!models.playerSettings.scaleToFit)
					Application.stage.align = StageAlign.TOP;

				Application.stage.displayState = StageDisplayState.FULL_SCREEN;
			}


			models.playerSettings.setFromConfig(); //notifys Observers

			//load main.swf's locale file
			var evLoadPlayerLocale:LoadDataEvent = new LoadDataEvent(LoadDataEvent.LOAD_PLAYER_TEXT);
			evLoadPlayerLocale.dispatch();

			//NOTE: moved to Re_loadLessonText
			/*
			   //proceed based on player type
			   //if single
			   if(models.playerSettings.playerType == PlayerSettings.SINGLE_LESSON){

			   var ev:LoadDataEvent = new LoadDataEvent(LoadDataEvent.RETRIEVE_LESSON_ID);
			   ev.dispatch();

			   if(models.playerSettings.useMainMenu) if(!models.playerStateModel.activated){
			   var evMenu:LoadDataEvent = new LoadDataEvent(LoadDataEvent.LOAD_MAIN_MENU);
			   evMenu.dispatch();
			   }

			   models.playerStateModel.activated = true;

			   }else { //assumes that a 'multi' player is always deployed on a CD requiring a processor check

			   var evChkProcessor:LoadDataEvent = new LoadDataEvent(LoadDataEvent.INTEL_PROCESSOR_CHECK);
			   evChkProcessor.dispatch();
			 }*/
			//END OF NOTE

			//important for garbage collection
			super.cleanup(e);
		}
	}

}